﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Scheme.Net.Runtime.Evaluator
{
    public class Trampoline<T>
    {
        private readonly Func<Trampoline<T>> _continuation;
        private readonly T _value;

        public Trampoline(T value) { _value = value; }
        public Trampoline(Func<Trampoline<T>> continuation) { _continuation = continuation; }

        public T Value
        {
            get
            {
                var lazy = this;
                while (lazy._continuation != null)
                {
                    lazy = lazy._continuation();
                }
                return lazy._value;
            }
        }
    }  
}
